import { createParamDecorator, ExecutionContext } from '@nestjs/common';
import { ExtraContext } from '../interfaces/extracontext.interface';
import { getRealIp } from '../tools/realip';

export const ExtraCtx = createParamDecorator((data: unknown, ctx: ExecutionContext): ExtraContext => {
  const extraData: ExtraContext = {};

  const request = ctx.switchToHttp().getRequest();
  const header = request.headers;

  // 获取客户端真实ip
  extraData.ip = getRealIp(request);

  // 获取token，在Bearer Token中
  const token = header.authorization?.replace('Bearer', '').trim(); // Bearer Token
  extraData.token = token;

  return extraData;
});
